<?php 
require_once('class.db.php');
class trangchu extends db {
	/* TAI KHOAN */
	function Register(){
		$firstname = trim($_POST['firstname']);
		$lastname = trim($_POST['lastname']);
		$password = $_POST['password'];
		//$xa_phuong = '';
		$tinh_tp = trim($_POST['tinhthanh']);
		$quan_huyen = trim($_POST['quanhuyen']);
		$email = trim($_POST['email']);
		$email_again = trim($_POST['email_again']);
		$date_create = date('Y-m-d H:i:s');
		//$birthday = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['date'];
		$gender = 0;
		$pattern = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$';
		$total = mysql_result($this->KiemTraEmail($email),0);
		$id = rand(10000,99999).rand(10000,99999).rand(10000,99999);
		if($firstname=='' || $lastname=='' || $password=='' || $tinh_tp=='' || $email=='' || $email_again=='' || ($email!=$email_again) || $total!=0) return false;
		elseif(!eregi($pattern, $email)) return false;
		else{
			$qr = "INSERT INTO `users` VALUES ('{$id}','{$firstname}','{$lastname}',MD5('{$password}'),NULL,'{$tinh_tp}','{$quan_huyen}',NULL,NULL,'{$email}','{$date_create}','2',NULL,'{$gender}','1','111111111',NULL,'0',NULL,NULL)";
			mysql_query($qr);/*insert users*/
			mysql_query("INSERT INTO `album` VALUES (NULL,'  Ảnh tường','{$id}','{$date_create}','','','1')");/*insert album default*/
			$id_ab = mysql_insert_id();
			mysql_query("INSERT INTO `album` VALUES (NULL,'  Ảnh đại diện','{$id}','{$date_create}','','','1')");/*insert album default*/
			mysql_query("INSERT INTO `friends` VALUES (NULL,'{$id}','','{$date_create}','0','')");/*insert friends default*/
			mysql_query("INSERT INTO `children` VALUES (NULL,'{$id}','controngbungme',NULL,NULL,NULL,NULL,NULL,NULL,'0','{$date_create}')");/*insert children default*/
			$_SESSION['idUser']		= $id;
			$_SESSION['name']		= $lastname.' '.$firstname;
			$_SESSION['avatar_user']= '111111111';
			$_SESSION['id_album']	= $id_ab;/*anh wall*/
			
			$anh_dd = mysql_query("SELECT id FROM `album` WHERE name='  Ảnh đại diện' AND user_id='".$_SESSION['idUser']."'");
			$row_anh_dd = mysql_fetch_array($anh_dd);
			$_SESSION['album_anh_daidien'] = $row_anh_dd['id'];/*anh dai dien*/
			include_once('guimail/register_send.php');
			return true;
		}
	}
	function KiemTraEmail($email){
		$qr = "SELECT COUNT(id) as Num FROM `users` WHERE email='{$email}'";
		return mysql_query($qr);
	}
	function KiemTraLogin(){
		$email = $_POST['txtUser'];
		$pass = md5($_POST['txtPass']);
		$qr = "SELECT users.id,concat(lastname,' ',firstname) as name,gender,avatar,album.id as album_id FROM `users`,`album` WHERE email='{$email}' AND password='{$pass}' AND active=1 AND group_id=2 AND album.name='  Ảnh tường' AND users.id=user_id";
		$u = mysql_query($qr);
		if(mysql_num_rows($u) > 0) {
			$row = mysql_fetch_array($u);
			$_SESSION['idUser']		= $row['id'];
			$_SESSION['name']		= $row['name'];
			$_SESSION['avatar_user']= $row['avatar'];
			$_SESSION['id_album']	= $row['album_id'];
			
			$anh_dd = mysql_query("SELECT id FROM `album` WHERE name='  Ảnh đại diện' AND user_id='".$_SESSION['idUser']."'");
			$row_anh_dd = mysql_fetch_array($anh_dd);
			$_SESSION['album_anh_daidien'] = $row_anh_dd['id'];
			/* update auto tuan thai */
			$info_be = $this->info_be($_SESSION['idUser']);
			$row_info_be = mysql_fetch_array($info_be);
			if($row_info_be['tuantuoi']>0 && $row_info_be['tuantuoi']<39){
				$songay_database = $row_info_be['tuantuoi'] * 7;
				$songay_phatsinh = $this->tinh_so_ngay(date('Y-m-d',strtotime($row_info_be['date_create'])),date('Y-m-d'),'-');
				$total_ngay = $songay_database + $songay_phatsinh;
				$sotuanmangthai = (int)($total_ngay / 7);
				if($row_info_be['tuantuoi'] != $sotuanmangthai) mysql_query("UPDATE children SET tuantuoi='".$sotuanmangthai."',date_create='".date('Y-m-d H:i:s')."' WHERE name='controngbungme' AND user_id='".$_SESSION['idUser']."'");
			}
			return true;
		} else {
			return false;
		}
	}
	function DoiMatKhau(){
		$password = $_POST['pass_cu'];
		$password_new = $_POST['pass_moi'];
		$password_repeat = $_POST['pass_nl'];
		if(strlen($password_new)<6) return 'Mật khẩu mới phải 6 ký tự trở lên';
		elseif($password_new!=$password_repeat) return 'Mật khẩu mới và mật khẩu nhắc lại không khớp';
		else{
			$pa = md5($password);
			$pa_new = md5($password_new);
			$qr = "SELECT id FROM users WHERE id='".$_SESSION['idUser']."' AND password='{$pa}'";
			$row = mysql_query($qr);
			if(mysql_num_rows($row) != 1) return 'Mật khẩu không đúng';
			else{
				$qr = mysql_query("UPDATE users SET password='{$pa_new}' WHERE id='".$_SESSION['idUser']."'");
				return true;
			}
		}
	}
	function seo($domain,$title,$description,$keywords,$url){
		return '<title>'.$title.'</title>
<base href="http://'.$domain.'/" />
<meta name="title" content="'.$title.'" />
<meta name="description" content="'.$description.'" />
<meta name="keywords" content="'.$keywords.'" />
<meta name="robots" content="INDEX,FOLLOW" />
<meta http-equiv="content-language" content ="vi" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="og:title" content="'.$title.'" />
<meta property="og:description" content="'.$description.'" />
<meta property="og:image" content="http://'.$domain.'/images/logo-loveheart.jpg" />
<meta property="og:url" content="http://'.$domain.$url.'" />
<link type="icon/x-icon" href="images/logo.ico" rel="shortcut icon" />';
	}
	/* Tinh thanh quan huyen */
	function tinh_thanh(){
		$qr = "SELECT id,name FROM ds_tinhthanh WHERE status=1 ORDER BY `order`";
		return mysql_query($qr);
	}
	function quan_huyen($id){
		$qr = "SELECT id,name FROM ds_quanhuyen WHERE status=1 AND tinhthanh_id='{$id}' ORDER BY `order`";
		return mysql_query($qr);
	}
	function name_tinh_tp($id){
		$qr = mysql_query("SELECT name FROM ds_tinhthanh WHERE status=1 AND id='{$id}'");
		$row = mysql_fetch_array($qr);
		return $row['name'];
	}
	function name_quan_huyen($id){
		$qr = mysql_query("SELECT name FROM ds_quanhuyen WHERE status=1 AND id='{$id}'");
		$row = mysql_fetch_array($qr);
		return $row['name'];
	}
	function slider(){
		$qr = "SELECT id,info FROM slider";
		return mysql_query($qr);
	}
	/* thong tin user */
	function user_info($id){
		$qr = "SELECT id,concat(lastname,' ',firstname) as name,tinh_tp,quan_huyen,congviec,hocvan,avatar FROM users WHERE active=1 AND id='{$id}'";
		return mysql_query($qr);
	}
	function friends($id){ //insert friend info
		$qr = mysql_query("SELECT friend FROM friends WHERE user_id='{$id}'");
		$row = mysql_fetch_array($qr);
		return $row['friend'];
	}
	function view_friend($user){ /*xem 1 friend*/
		$qr = "SELECT user_id,friend FROM friends WHERE user_id='{$user}'";
		return mysql_query($qr);
	}
	function view_friends($user){ /*so luong friend*/
		$qr = "SELECT users.id,concat(firstname,' ',lastname) as name,avatar,friend FROM users,friends WHERE active=1 AND users.id='{$user}' AND users.id=user_id";
		return mysql_query($qr);
	}
	function kiemtra_ketban($user,$friend){
		$qr  = mysql_query("SELECT id FROM friends WHERE user_id='{$user}' AND friend LIKE '%{$friend},%'");
		$qr2 = mysql_query("SELECT id FROM friends WHERE user_id='{$user}' AND doitraloi LIKE '%{$friend},%'");
		$qr3 = mysql_query("SELECT id FROM friends WHERE user_id='{$friend}' AND doitraloi LIKE '%{$user},%'");
		if(mysql_num_rows($qr3)==1) $view='<a href="javascript:;" class="xacnhan_one '.$friend.'"><span style="font-weight:100; font-style:italic">Xác nhận</span></a>';
		elseif(mysql_num_rows($qr)==1) $view='';
		elseif(mysql_num_rows($qr2)==1) $view='<a href="javascript:;"><span style="font-weight:100; font-style:italic">Đã gửi lời mời</span></a>';
		elseif($user!=$friend) $view='<a href="index.html?user='.$friend.'&type=ketban">Kết bạn</a>';
		return $view;
	}
	function ketban($user,$friend){
		if($user!=$friend){
			$qr = mysql_query("SELECT doitraloi FROM friends WHERE user_id='{$user}' AND (doitraloi NOT LIKE '%{$friend},%' AND friend NOT LIKE '%{$friend},%')");
			if(mysql_num_rows($qr)==1){
				$row = mysql_fetch_array($qr);
				$ketban = $row['doitraloi'].$friend.',';
				$qr = mysql_query("UPDATE friends SET doitraloi='{$ketban}' WHERE user_id='{$user}'");
			}
		}
		
	}
	function ds_traloi_ketban($user){
		$qr = "SELECT user_id,friend,doitraloi FROM friends WHERE doitraloi LIKE '%{$user},%'";
		return mysql_query($qr);
	}
	function friend_xacnhan($id){
		$qr = mysql_query("SELECT friend,doitraloi FROM friends WHERE user_id='{$id}'");
		$row = mysql_fetch_array($qr);
		$xoa_traloi = str_replace($_SESSION['idUser'].',','',$row['doitraloi']);
		$them_friend2 = $row['friend'].$_SESSION['idUser'].',';
		
		$qr2 = mysql_query("SELECT friend FROM friends WHERE friend NOT LIKE '%$id,%' AND user_id='".$_SESSION['idUser']."'");
		$row2 = mysql_fetch_array($qr2);
		$them_friend = $row2['friend'].$id.',';
		mysql_query("UPDATE friends SET doitraloi='{$xoa_traloi}' WHERE user_id='{$id}'");
		mysql_query("UPDATE friends SET friend='{$them_friend}' WHERE user_id='".$_SESSION['idUser']."'");
		mysql_query("UPDATE friends SET friend='{$them_friend2}' WHERE user_id='{$id}'");
	}
	function friend_huy($id){
		$qr = mysql_query("SELECT doitraloi FROM friends WHERE user_id='{$id}'");
		$row = mysql_fetch_array($qr);
		$xoa_traloi = str_replace($_SESSION['idUser'].',','',$row['doitraloi']);
		mysql_query("UPDATE friends SET doitraloi='{$xoa_traloi}' WHERE user_id='{$id}'");
	}
	function info_be($user){
		$qr = "SELECT name_that,name_onha,gender,bethu,tuantuoi,date_create FROM children WHERE name='controngbungme' AND user_id='{$user}'";
		return mysql_query($qr);
	}
	function view_tuanthai($id){
		$qr = mysql_query("SELECT name FROM tuanthai WHERE id='{$id}'");
		$row = mysql_fetch_array($qr);
		return $row['name'];
	}
	function list_tuanthai(){
		$qr = "SELECT id,name FROM tuanthai ORDER BY id";
		return mysql_query($qr);
	}
	function update_info_be(){
		$name_that = $_POST['name_that'];
		$name_onha = $_POST['name_onha'];
		$gender = $_POST['gender'];
		$bethu = $_POST['bethu'];
		$tuantuoi = $_POST['tuantuoi'];
		$tuan_c = $_POST['tuan_c'];
		if($tuantuoi!=$tuan_c) $date = ",date_create='".date('Y-m-d H:i:s')."'";
		$qr = "UPDATE children SET name_that='{$name_that}',name_onha='{$name_onha}',gender='{$gender}',bethu='{$bethu}',tuantuoi='{$tuantuoi}'{$date} WHERE name='controngbungme' AND user_id='".$_SESSION['idUser']."'";
		mysql_query($qr);
	}
	function info_me($user){
		$qr = "SELECT firstname,lastname,xa_phuong,tinh_tp,quan_huyen,birthday,congviec,hocvan,email FROM users WHERE active=1 AND id='{$user}'";
		return mysql_query($qr);
	}
	function update_info_me(){
		$lastname = $_POST['lastname'];
		$firstname = $_POST['firstname'];
		$birthday = $_POST['nam'].'-'.$_POST['thang'].'-'.$_POST['ngay'];
		$xa_phuong = $_POST['xa_phuong'];
		$tinh_tp = $_POST['tinh_tp'];
		$quan_huyen = $_POST['quan_huyen'];
		$congviec = $_POST['congviec'];
		$hocvan = $_POST['hocvan'];
		$qr = "UPDATE users SET lastname='{$lastname}',firstname='{$firstname}',birthday='{$birthday}',xa_phuong='{$xa_phuong}',tinh_tp='{$tinh_tp}',quan_huyen='{$quan_huyen}',congviec='{$congviec}',hocvan='{$hocvan}' WHERE id='".$_SESSION['idUser']."'";
		mysql_query($qr);
	}
	function add_children(){
		$name = $_POST['name'];
		$ngaysinh = $_POST['nam'].'-'.$_POST['thang'].'-'.$_POST['ngay'];
		$congviec = $_POST['congviec'];
		$gender = $_POST['gender'];
		$qr = "INSERT INTO children VALUES (NULL,'".$_SESSION['idUser']."','{$name}',NULL,NULL,'{$ngaysinh}','{$congviec}','{$gender}','','','".date('Y-m-d H:i:s')."')";
		mysql_query($qr);
	}
	function update_children($id){
		$name = $_POST['name'];
		$ngaysinh = $_POST['nam'].'-'.$_POST['thang'].'-'.$_POST['ngay'];
		$congviec = $_POST['congviec'];
		$gender = $_POST['gender'];
		$qr = "UPDATE children SET name='{$name}',ngaysinh='{$ngaysinh}',congviec='{$congviec}',gender='{$gender}' WHERE name<>'controngbungme' AND id='{$id}'";
		mysql_query($qr);
	}
	function ds_children($user){
		$qr = "SELECT id,name,ngaysinh,congviec FROM children WHERE name<>'controngbungme' AND user_id='{$user}' ORDER BY ngaysinh DESC";
		return mysql_query($qr);
	}
	function one_children($id){
		$qr = "SELECT name,ngaysinh,congviec,gender FROM children WHERE name<>'controngbungme' AND id='{$id}'";
		return mysql_query($qr);
	}
	/* chat */
	function cungtuanthai(){
		$qr = "";
		return mysql_query($qr);
	}
	function ds_cung_quan($quan_huyen){
		$qr = "SELECT id,concat(lastname,' ',firstname) as name,avatar FROM users WHERE active=1 AND quan_huyen='{$quan_huyen}' AND id<>'".$_SESSION['idUser']."'";
		return mysql_query($qr);
	}
	function ds_cung_tuan_thai($tuan_thai){
		$qr = "SELECT users.id,concat(lastname,' ',firstname) as name,avatar FROM users,children WHERE active=1 AND children.name='controngbungme' AND tuantuoi='{$tuan_thai}' AND users.id=user_id AND user_id<>'".$_SESSION['idUser']."'";
		return mysql_query($qr);
	}
	/* Wall */
	function insert_info($user_id,$content,$photo_name,$share,$date_create){
		$qr = "INSERT INTO info VALUES (NULL,'{$user_id}','{$content}','{$photo_name}','{$share}','','{$date_create}')";
		mysql_query($qr);
		$id = mysql_insert_id();
		return $id;
	}
	function insert_info_comment($info_id){
		$qr = "INSERT INTO info_comment VALUES (NULL,'{$info_id}','','".$_SESSION['idUser']."','')";
		mysql_query($qr);
	}
	function insert_news($content,$date_create,$info_id,$photo_id,$notification){
		$status = substr($content, 0, 90); if(strlen($content)>90) $status .= '...';
		$status = str_replace('<br />',' ',$status);
		$qr_news = "INSERT INTO news VALUES (NULL,'".$_SESSION['idUser']."','{$status}','{$date_create}','{$info_id}','{$photo_id}','{$notification}')";
		mysql_query($qr_news);
		/*if($notification!=''){
			$url_link = 'http://www.umyli.com/index.html?user='.$_SESSION['idUser'].'&type=detail&id='.$info_id;
			$list_email = explode(' |-| ', $notification);
			$email = array();
			for($i=0; $i<count($list_email); $i++){
				if($_SESSION['idUser'] != $list_email[$i]){
					$user_mail = $tc->user_info($list_email[$i]);
					$row_user = mysql_fetch_array($user_mail);
					$email[] = $row_user['email'];
				}
			}
			include_once('guimail/send_wall.php');
		}*/
		return true;
	}
	function insert_wall_status($ct,$share,$photo_name,$user){
		$m = explode('http://',$ct);
		$total = count($m);
		if($total!=1){
			$content .= $m[0];
			for($i=1; $i<$total; $i++){
				$mm = explode(' ',$m[$i],2);
				$content .= '<a href="http://'.$mm[0].'" target="_blank">http://'.$mm[0].'</a> '.$mm[1];
			}
		}else $content=$ct;
		
		$user_id = $_SESSION['idUser'];
		$date_create = date('Y-m-d H:i:s');
		if($share==1){
			if($user_id==$user){
				$friends = $this->friends($user_id);
				$user_insert = $user_id.','.$friends;
			}else $user_insert = $user_id.','.$user;
			
			$info_id = $this->insert_info($user_insert,$content,$photo_name,$share,$date_create);
			$this->insert_info_comment($info_id);
			if($content=='' && $photo_name!='') $content .= ' vừa đăng ảnh của cô ấy.';
			$this->insert_news($content,$date_create,$info_id,0,$notification);
		}elseif($share==3){
			$info_id = $this->insert_info($user_id,$content,$photo_name,$share,$date_create);
			$this->insert_info_comment($info_id);
		}
	}
	function view_wall_info($user,$from){
		$qr = "SELECT id,user_id,content,photo_name,share_id,user_like,date_create FROM info WHERE user_id LIKE '%{$user}%' ORDER BY date_create DESC LIMIT {$from},".max_wall;
		return mysql_query($qr);
	}
	function view_wall_info_one($user,$info_id){
		$qr = "SELECT id,user_id,content,photo_name,share_id,user_like,date_create FROM info WHERE id='{$info_id}' ";
		return mysql_query($qr);
	}
	function view_wall_new($user){
		$qr = "SELECT user_id,concat(lastname,' ',firstname) as name,status,avatar,info_id,photo_id FROM news,users WHERE user_id<>'{$user}' AND user_id=users.id ORDER BY news.date_create DESC LIMIT 50";
		return mysql_query($qr);
	}
	function insert_comment($id,$comment){
		$user_id = $_SESSION['idUser'];
		$date_create = date('Y-m-d H:i:s');
		$check = $this->view_comment($id);
		$row = mysql_fetch_array($check);
		
		$notification = $row['user_id'];
		$comment = trim($comment);
		$comment = str_replace('&','&amp;',$comment);
		$comment = str_replace('<','&lt;',$comment);
		$comment = str_replace('>','&gt;',$comment);
		
		$comment1 = $row['comment'].' |-| '.$comment;
		$user_id1 = $row['user_id'].' |-| '.$user_id;
		$date_create1 = $row['date_create'].' |-| '.$date_create;
		$qr = "UPDATE info_comment SET comment='{$comment1}',user_id='{$user_id1}',date_create='{$date_create1}' WHERE info_id='{$id}'";
		mysql_query($qr);
		
		$this->insert_news($comment,$date_create,$id,0,$notification);
	}
	function view_comment($id){
		$qr = "SELECT comment,user_id,date_create FROM info_comment WHERE info_id='{$id}'";
		return mysql_query($qr);
	}
	function insert_user_like($id_info,$id_user){
		$id_user = $id_user.' || ';
		$qr = mysql_query("SELECT user_like FROM info WHERE id='{$id_info}'");
		$row = mysql_fetch_array($qr);
		$m = explode($id_user,$row['user_like']);
		if(count($m)==1){
			$str = $row['user_like'].$id_user;
			mysql_query("UPDATE info SET user_like='{$str}' WHERE id='{$id_info}'");
		}else{
			$str = str_replace($id_user,'',$row['user_like']);
			mysql_query("UPDATE info SET user_like='{$str}' WHERE id='{$id_info}'");
		}
	}
	
	function insert_user_like_photo($id_info,$id_user){
		$id_user = $id_user.' || ';
		$qr = mysql_query("SELECT user_like FROM photos WHERE id='{$id_info}'");
		$row = mysql_fetch_array($qr);
		$m = explode($id_user,$row['user_like']);
		if(count($m)==1){
			$str = $row['user_like'].$id_user;
			mysql_query("UPDATE photos SET user_like='{$str}' WHERE id='{$id_info}'");
		}else{
			$str = str_replace($id_user,'',$row['user_like']);
			mysql_query("UPDATE photos SET user_like='{$str}' WHERE id='{$id_info}'");
		}
	}
	function check_like($id,$user){
		$qr = mysql_query("SELECT id FROM info WHERE id='{$id}' AND user_like LIKE '%{$user} || %'");
		return mysql_num_rows($qr);
	}
	function check_like_photos($id,$user){
		$qr = mysql_query("SELECT id FROM photos WHERE id='{$id}' AND user_like LIKE '%{$user} || %'");
		return mysql_num_rows($qr);
	}
	function info_delete($id){
		$qr = mysql_query("SELECT user_id FROM info WHERE id='{$id}'");
		if(mysql_num_rows($qr)==1){
			$row = mysql_fetch_array($qr);
			$m = explode(',',$row['user_id'],2);
			if($m[0]==$_SESSION['idUser']){
				mysql_query("DELETE FROM info WHERE id='{$id}'");
				mysql_query("DELETE FROM info_comment WHERE info_id='{$id}'");
				mysql_query("DELETE FROM news WHERE info_id='{$id}'");
			}else{
				$str = str_replace($_SESSION['idUser'].',','',$row['user_id']);
				mysql_query("UPDATE info SET user_id='{$str}' WHERE id='{$id}'");
			}
		}
	}
	function view_photo($w,$h,$name){
		$qr = mysql_query("SELECT id,album_id,user_id FROM photos WHERE name='{$name}'");
		$row = mysql_fetch_array($qr);
		if($h==320) $url=url_hinh_photo_480x320; else $url=url_hinh_photo_300x200;
		return '<a href="index.html?user='.$row['user_id'].'&type=all-pic&album='.$row['album_id'].'&id='.$row['id'].'"><img width="'.$w.'" height="'.$h.'" src="'.$url.$name.'.jpg" /></a>';
	}
	/* chuyen doi ngay gio */
	function wall_datetime($datetime){
		$m = explode(' ',$datetime);
		$date = explode('-',$m[0]); $date = $date[2].' tháng '.$date[1].' | ';
		$time = date('H:i',strtotime($m[1]));
		return $date.$time;
	}
	function wall_date($date){
		$m = explode(' ',$date);
		$date = explode('-',$m[0]); $date = $date[2].' tháng '.$date[1];
		return $date;
	}
	function tinh_so_ngay($beginDate,$endDate,$dformat){
		$date_parts1=explode($dformat,$beginDate);
		$date_parts2=explode($dformat,$endDate);
		$start_date=gregoriantojd($date_parts1[1],$date_parts1[2],$date_parts1[0]);/* ($thang,$ngay,$nam) */
		$end_date=gregoriantojd($date_parts2[1],$date_parts2[2],$date_parts2[0]);
		return $end_date - $start_date;
	}
	/* photos album */
	function view_photo_album($user){
		$qr = "SELECT * FROM album WHERE user_id='{$user}' ORDER BY date_create DESC";
		return mysql_query($qr);
	}
	function create_album($name,$share){
		$name = trim($name);
		$idUser = $_SESSION['idUser'];
		$date = date('Y-m-d H:i:s');
		if($name!='' && $idUser!='' && ($share==1 || $share==3)){
			$qr = "INSERT INTO album VALUES (NULL,'{$name}','{$idUser}','{$date}',NULL,NULL,'{$share}')";
			mysql_query($qr);
			$_SESSION['create_id_album'] = mysql_insert_id();
			return true;
		}
	}
	function anhdaidien_album($id){//set post anh
		$qr = "UPDATE album SET anhdaidien='{$id}' WHERE id='".$_SESSION['create_id_album']."'";
		mysql_query($qr);
	}
	function set_anhdaidien_album($name,$album){//edit
		$qr = "UPDATE album SET anhdaidien='{$name}' WHERE id='{$album}' AND user_id='".$_SESSION['idUser']."'";
		mysql_query($qr);
		if($album==$_SESSION['album_anh_daidien']){
			$user130x130 = url_hinh_user_130x130.$_SESSION['avatar_user'].'.jpg';
			$user40x40 = url_hinh_user_40x40.$_SESSION['avatar_user'].'.jpg';
			if(file_exists($user130x130) && $_SESSION['avatar_user']!='111111111') unlink($user130x130);
			if(file_exists($user40x40) && $_SESSION['avatar_user']!='111111111') unlink($user40x40);
			
			 /*PHPThumb */
			require_once('library/PHPThumb/ThumbLib.inc.php');
			$file = url_hinh_photo_full.$name.'.jpg';
			$thumb = PhpThumbFactory::create($file);
			$thumb->adaptiveResize(130,130);
			$thumb->save(url_hinh_user_130x130.$name.'.jpg');
			
			/* anh url_hinh_user_40x40 */
			require_once('SimpleImage.php');
			$image = new SimpleImage();
			$image->load(url_hinh_user_130x130.$name.'.jpg');
			$image->resizeToWidth(130);
			$image->save(url_hinh_user_130x130.$name.'.jpg');
			$image->load(url_hinh_user_130x130.$name.'.jpg');
			$image->resizeToWidth(40);
			$image->save(url_hinh_user_40x40.$name.'.jpg');
			$_SESSION['avatar_user'] = $name;
			mysql_query("UPDATE users SET avatar='{$name}' WHERE id='".$_SESSION['idUser']."'");
		}
	}
	function album_rand_anhdd($id){
		$qr = mysql_query("SELECT name FROM photos WHERE album_id='{$id}' ORDER BY rand()");
		if(mysql_num_rows($qr) > 0){
			$row = mysql_fetch_array($qr);
			mysql_query("UPDATE album SET anhdaidien='{$row['name']}' WHERE id='{$id}'");
			return $row['name'];
		}else return no_image;
	}
	function all_pic($album,$user){
		$qr = "SELECT id,name FROM photos WHERE album_id='{$album}' AND user_id='{$user}' ORDER BY date_create DESC";
		return mysql_query($qr);
	}
	function album_info($id){
		$qr = "SELECT name FROM album WHERE id='{$id}'";
		return mysql_query($qr);
	}
	function photo_one($id,$user){
		$qr = "SELECT id,name,date_create,album_id,user_id,info,user_like FROM photos WHERE id='{$id}' AND user_id='{$user}'";
		return mysql_query($qr);
	}
	function view_comment_photo($id){
		$qr = "SELECT comment,user_id,date_create FROM photos_comment WHERE photos_id='{$id}'";
		return mysql_query($qr);
	}
	function insert_comment_photo($id,$comment){
		$user_id = $_SESSION['idUser'];
		$date_create = date('Y-m-d H:i:s');
		$check = $this->view_comment_photo($id);
		$row = mysql_fetch_array($check);
		
		$comment = trim($comment);
		$comment = str_replace('&','&amp;',$comment);
		$comment = str_replace('<','&lt;',$comment);
		$comment = str_replace('>','&gt;',$comment);
		
		$comment1 = $row['comment'].' |-| '.$comment;
		$user_id1 = $row['user_id'].' |-| '.$user_id;
		$date_create1 = $row['date_create'].' |-| '.$date_create;
		$qr = "UPDATE photos_comment SET comment='{$comment1}',user_id='{$user_id1}',date_create='{$date_create1}' WHERE photos_id='{$id}'";
		mysql_query($qr);
		
		$this->insert_news($comment,$date_create,0,$id,'');
	}
	/*set size image */
	function getPictureSize($filename){
		$size_info = getimagesize($filename);
		$width = $size_info[0];
		$height = $size_info[1];
		if($width>$height && $width>1000) return 1000;
		elseif($width>$height && $width>450 && $width<1000) return $width;
		elseif($width>$height && $width<500) return $width;
		elseif($width<$height && $height>700) return 700;
		elseif($width<$height && $height>300 && $height<700) return $height;
		elseif($width<$height && $height<300) return $height;
	}
	function add_nhatky(){
		$info = trim($_POST['content']);
		$name = trim($_POST['name']);
		$share = $_POST['share'];
		if($info!='' && ($share==1 || $share==3)) $qr = mysql_query("INSERT INTO album VALUES (NULL,'{$name}','".$_SESSION['idUser']."','".date('Y-m-d H:i:s')."','{$info}',NULL,'{$share}')");
		else return false;
	}
	function search($str){
		$m = explode('@',$str);
		if(count($m)==2) $qr = "SELECT id,concat(lastname,' ',firstname) as name,avatar,hocvan,congviec FROM users WHERE email='{$str}'";
		else{
			$m = explode(' ',$str); $total = count($m);
			if($total==1) $qr = "SELECT id,concat(lastname,' ',firstname) as name,avatar,hocvan,congviec FROM users WHERE lastname LIKE '%{$m[0]}%' OR firstname LIKE '%{$m[0]}%' LIMIT 10";
			else{
				$firstname = $total - 1;
				for($i = 0; $i<($total-1); $i++) {
					$lastname .= $m[$i].' ';
				}
				$qr = "SELECT id,concat(lastname,' ',firstname) as name,avatar,hocvan,congviec FROM users WHERE (lastname LIKE '%".trim($lastname)."%') OR (firstname LIKE '{".$m[$firstname]."}%') LIMIT 10";
			}
		}
		return mysql_query($qr);
	}
	function resetpass($email){
		$qr = mysql_query("SELECT RandomKey FROM users WHERE email='{$email}'");
		if(mysql_num_rows($qr)==1){
			$row = mysql_fetch_array($qr);
			$key = $row['RandomKey'];
			if($key==''){
				$RandomKey = rand(10000,9999999).rand(10000,9999999).rand(10000,9999999);
				mysql_query("UPDATE users SET RandomKey='$RandomKey' WHERE active=1 AND email='{$email}'");
				$url_link = 'http://www.umyli.com/index.html?user=11223344556688&type=reset&id='.$RandomKey;
				$url_link_xoa = 'http://www.umyli.com/index.html?user=11223344556688&type=reset&id='.$RandomKey.'&del=1';
				include_once('guimail/send.php');
				$message = 1;
			}else $message = 2;
		}else $message = 3;
		return $message;
	}
	function check_reset($key){
		$qr = mysql_query("SELECT id FROM users WHERE RandomKey='{$key}'");
		if(mysql_num_rows($qr)==1) return '1'; else return '0';
	}
	function thaydoimatkhau($pass1,$key){
		$pass = md5($pass1);
		mysql_query("UPDATE users SET password='{$pass}',RandomKey='' WHERE active=1 AND RandomKey='{$key}'");
	}
	function delete_reset_pass($key){
		mysql_query("UPDATE users SET RandomKey='' WHERE active=1 AND RandomKey='{$key}'");
	}
	function notification($idUser){
		$qr = "SELECT user_id,status,info_id FROM news WHERE user_id<>'{$idUser}' AND notification LIKE '%{$idUser}%' ORDER BY id DESC LIMIT 30";
		return mysql_query($qr);
	}
}// end trangchu